class MyPage extends BaseList {
    constructor(action, orderby, flag, $pagesize, $pagination, cokiePageSize, cokiePageIndex, cokieListResult, listUrl, delUrl) {
        super(action, orderby, flag, $pagesize, $pagination, cokiePageSize, cokiePageIndex, cokieListResult, listUrl, delUrl);
    }
    OnPageLoad() {
        // 设置初始默认值
        let _currentDate = new Date();
        $("#YearOfBill").val(_currentDate.getFullYear());
        if ((_currentDate.getMonth() + 1) < 10) {
            $("#MonthOfBill").val("0" + "" + (_currentDate.getMonth() + 1));
        } else {
            $("#MonthOfBill").val(_currentDate.getMonth() + 1);
        }

        // 回车直接进行搜索
        $("panel_search").bind('keyup', function (e) {
            var key = e.keyCode;
            if (key === 13) {
                _Instance.Fetch();
            }
        });
        $('#loading').hide();
    }
    SetCriterion(item) {
        item['YearOfInvoice'] = $("#YearOfBill").val();
        item['MonthOfInvoice'] = $("#MonthOfBill").val();
        item['InvoiceNo'] = $.trim($("#InvoiceNo").val()) === '' ? null : $.trim($("#InvoiceNo").val());
        item['SupplierName'] = $.trim($("#SupplierName").val()) === '' ? null : $.trim($("#SupplierName").val());
        item['MonthOfPayed'] = $("#MonthOfPayed").val() === '-1' ? null : $("#MonthOfPayed").val();
        item['SubTotalType'] = parseInt($("#HasSubTotal").val());
        return item;
    }
    GetFilter() {
        let item = super.GetFilter(0);
        // 设置条件
        item = this.SetCriterion(item);
        return JSON.stringify(item);
    }
    EmptyTableContent() {
        $("#tab_data01 tbody").empty();
    }
    SetTableContent(result) {
        $("#tab_data01 tbody").empty();

        let _totalAmountAddedTax = 0;                 // 材料含税总金额
        let _totalAmountExcludedTax = 0;              // 材料不含税总金额
        let _totalAmountNonMaterialAddedTax = 0;      // 非材料含税总金额
        let _totalAmountNonMaterialExcludedTax = 0;   // 非材料不含税总金额

        let content_01 = '';
        switch (parseInt($("#HasSubTotal").val())) {
            case 0:
                for (let i = 0; i < result.length; i++) {
                    if (parseInt(result[i].rowFlag) === 0) {
                        content_01 += '<tr >';
                        content_01 += '<td >' + formatNullData(result[i].invoiceNo) + '</td>';
                        content_01 += '<td>' + formatNullData(result[i].supplierName) + '</td>';
                        content_01 += '<td>' + formatNullData(result[i].styleNo) + '</td>';
                        content_01 += '<td>' + formatNullData(result[i].poNo) + '</td>';
                        content_01 += '<td style="text-align:right;">' + formatNullData(result[i].materialAmountAddedTax) + '</td>';
                        content_01 += '<td style="text-align:right;">' + formatNullData(result[i].materialAmountExcludedTax) + '</td>';
                        if (result[i].materialTaxRate !== null && result[i].materialTaxRate !== 0) {
                            content_01 += '<td style="text-align:right;">' + formatNullData(result[i].materialTaxRate * 100) + '%</td>';
                        } else {
                            content_01 += '<td></td>';
                        }
                        content_01 += '<td style="text-align:right;">' + formatNullData(result[i].nonMaterialAmountAddedTax) + '</td>';
                        content_01 += '<td style="text-align:right;">' + formatNullData(result[i].nonMaterialAmountExcludedTax) + '</td>';
                        if (result[i].nonMaterialTaxRate !== null && result[i].nonMaterialTaxRate !== 0) {
                            content_01 += '<td style="text-align:right;">' + formatNullData(result[i].nonMaterialTaxRate * 100) + '%</td>';
                        } else {
                            content_01 += '<td></td>';
                        }
                        content_01 += '<td>' + formatNullData(result[i].materialName) + '</td>';
                        content_01 += '<td>' + formatNullData(result[i].monthOfBill) + '</td>';
                        content_01 += '<td>' + formatNullData(result[i].monthOfPayed.replace(/\b(0+)/gi, "")) + '</td>';
                        content_01 += '</tr>';
                        $("#tab_data01 tbody").append(content_01);
                        content_01 = '';
                        this.rows = this.rows + 1;
                    }
                    // 计算总金额
                    if (parseInt(result[i].rowFlag) === 9) {
                        _totalAmountAddedTax = _totalAmountAddedTax + (result[i].materialAmountAddedTax === null ? 0 : result[i].materialAmountAddedTax);    // 含税总金额
                        _totalAmountExcludedTax = _totalAmountExcludedTax + (result[i].materialAmountExcludedTax === null ? 0 : result[i].materialAmountExcludedTax);    // 不含税总金额
                        _totalAmountNonMaterialAddedTax = _totalAmountNonMaterialAddedTax + (result[i].nonMaterialAmountAddedTax === null ? 0 : result[i].nonMaterialAmountAddedTax);    // 含税总金额
                        _totalAmountNonMaterialExcludedTax = _totalAmountNonMaterialExcludedTax + (result[i].nonMaterialAmountExcludedTax === null ? 0 : result[i].nonMaterialAmountExcludedTax);    // 不含税总金额
                    }
                }
                break;
            case 1:
                for (let i = 0; i < result.length; i++) {
                    if (parseInt(result[i].rowFlag) === 0) {
                        content_01 += '<tr >';
                        content_01 += '<td >' + formatNullData(result[i].invoiceNo) + '</td>';
                        content_01 += '<td>' + formatNullData(result[i].supplierName) + '</td>';
                        content_01 += '<td>' + formatNullData(result[i].styleNo) + '</td>';
                        content_01 += '<td>' + formatNullData(result[i].poNo) + '</td>';
                        content_01 += '<td>' + formatNullData(result[i].materialAmountAddedTax) + '</td>';
                        content_01 += '<td>' + formatNullData(result[i].materialAmountExcludedTax) + '</td>';
                        if (result[i].materialTaxRate !== null && result[i].materialTaxRate !== 0) {
                            content_01 += '<td>' + formatNullData(result[i].materialTaxRate * 100) + '%</td>';
                        } else {
                            content_01 += '<td></td>';
                        }
                        content_01 += '<td>' + formatNullData(result[i].nonMaterialAmountAddedTax) + '</td>';
                        content_01 += '<td>' + formatNullData(result[i].nonMaterialAmountExcludedTax) + '</td>';
                        if (result[i].nonMaterialTaxRate !== null && result[i].nonMaterialTaxRate !== 0) {
                            content_01 += '<td>' + formatNullData(result[i].nonMaterialTaxRate * 100) + '%</td>';
                        } else {
                            content_01 += '<td></td>';
                        }
                        content_01 += '<td>' + formatNullData(result[i].materialName) + '</td>';
                        content_01 += '<td style="text-align:center;">' + formatNullData(result[i].monthOfBill) + '</td>';
                        content_01 += '<td style="text-align:center;">' + formatNullData(result[i].monthOfPayed.replace(/\b(0+)/gi, "")) + '</td>';
                        content_01 += '</tr>';
                        $("#tab_data01 tbody").append(content_01);
                        content_01 = '';
                        this.rows = this.rows + 1;
                    }
                    if (parseInt(result[i].rowFlag) === 1) {
                        content_01 += '<tr style="background-color:#ffe4b5; color:#000">';
                        content_01 += '<td colspan="4"></td>';
                        content_01 += '<td>' + formatNullData(result[i].materialAmountAddedTax) + '</td>';
                        content_01 += '<td>' + formatNullData(result[i].materialAmountExcludedTax) + '</td>';
                        content_01 += '<td></td>';
                        content_01 += '<td>' + formatNullData(result[i].nonMaterialAmountAddedTax) + '</td>';
                        content_01 += '<td>' + formatNullData(result[i].nonMaterialAmountExcludedTax) + '</td>';
                        content_01 += '<td></td>';
                        content_01 += '<td colspan="3"></td>';
                        content_01 += '</tr>';
                        $("#tab_data01 tbody").append(content_01);
                        content_01 = '';
                        this._rows = this.rows + 1;
                    }
                    // 计算总金额
                    if (parseInt(result[i].rowFlag) === 9) {
                        _totalAmountAddedTax = _totalAmountAddedTax + (result[i].materialAmountAddedTax === null ? 0 : result[i].materialAmountAddedTax);    // 含税总金额
                        _totalAmountExcludedTax = _totalAmountExcludedTax + (result[i].materialAmountExcludedTax === null ? 0 : result[i].materialAmountExcludedTax);    // 不含税总金额
                        _totalAmountNonMaterialAddedTax = _totalAmountNonMaterialAddedTax + (result[i].nonMaterialAmountAddedTax === null ? 0 : result[i].nonMaterialAmountAddedTax);    // 含税总金额
                        _totalAmountNonMaterialExcludedTax = _totalAmountNonMaterialExcludedTax + (result[i].nonMaterialAmountExcludedTax === null ? 0 : result[i].nonMaterialAmountExcludedTax);    // 不含税总金额
                    }
                }
                break;
            case 2:
                for (let i = 0; i < result.length; i++) {
                    if (parseInt(result[i].rowFlag) === 0) {
                        content_01 += '<tr >';
                        content_01 += '<td >' + formatNullData(result[i].invoiceNo) + '</td>';
                        content_01 += '<td>' + formatNullData(result[i].supplierName) + '</td>';
                        content_01 += '<td>' + formatNullData(result[i].styleNo) + '</td>';
                        content_01 += '<td>' + formatNullData(result[i].poNo) + '</td>';
                        content_01 += '<td>' + formatNullData(result[i].materialAmountAddedTax) + '</td>';
                        content_01 += '<td>' + formatNullData(result[i].materialAmountExcludedTax) + '</td>';
                        if (result[i].materialTaxRate !== null && result[i].materialTaxRate !== 0) {
                            content_01 += '<td>' + formatNullData(result[i].materialTaxRate * 100) + '%</td>';
                        } else {
                            content_01 += '<td></td>';
                        }
                        content_01 += '<td>' + formatNullData(result[i].nonMaterialAmountAddedTax) + '</td>';
                        content_01 += '<td>' + formatNullData(result[i].nonMaterialAmountExcludedTax) + '</td>';
                        if (result[i].nonMaterialTaxRate !== null && result[i].nonMaterialTaxRate !== 0) {
                            content_01 += '<td>' + formatNullData(result[i].nonMaterialTaxRate * 100) + '%</td>';
                        } else {
                            content_01 += '<td></td>';
                        }
                        content_01 += '<td>' + formatNullData(result[i].materialName) + '</td>';
                        content_01 += '<td>' + formatNullData(result[i].monthOfBill) + '</td>';
                        content_01 += '<td>' + formatNullData(result[i].monthOfPayed.replace(/\b(0+)/gi, "")) + '</td>';
                        content_01 += '</tr>';
                        $("#tab_data01 tbody").append(content_01);
                        content_01 = '';
                        this.rows = this.rows + 1;
                    }
                    if (parseInt(result[i].rowFlag) === 2) {
                        content_01 += '<tr style="background-color:#ffe4b5; color:#000">';
                        content_01 += '<td colspan="4"></td>';
                        content_01 += '<td>' + formatNullData(result[i].materialAmountAddedTax) + '</td>';
                        content_01 += '<td>' + formatNullData(result[i].materialAmountExcludedTax) + '</td>';
                        content_01 += '<td></td>';
                        content_01 += '<td>' + formatNullData(result[i].nonMaterialAmountAddedTax) + '</td>';
                        content_01 += '<td>' + formatNullData(result[i].nonMaterialAmountExcludedTax) + '</td>';
                        content_01 += '<td></td>';
                        content_01 += '<td colspan="3"></td>';
                        content_01 += '</tr>';
                        $("#tab_data01 tbody").append(content_01);
                        content_01 = '';
                        this._rows = this._rows + 1;
                    }
                    // 计算总金额
                    if (parseInt(result[i].rowFlag) === 9) {
                        _totalAmountAddedTax = _totalAmountAddedTax + (result[i].materialAmountAddedTax === null ? 0 : result[i].materialAmountAddedTax);    // 含税总金额
                        _totalAmountExcludedTax = _totalAmountExcludedTax + (result[i].materialAmountExcludedTax === null ? 0 : result[i].materialAmountExcludedTax);    // 不含税总金额
                        _totalAmountNonMaterialAddedTax = _totalAmountNonMaterialAddedTax + (result[i].nonMaterialAmountAddedTax === null ? 0 : result[i].nonMaterialAmountAddedTax);    // 含税总金额
                        _totalAmountNonMaterialExcludedTax = _totalAmountNonMaterialExcludedTax + (result[i].nonMaterialAmountExcludedTax === null ? 0 : result[i].nonMaterialAmountExcludedTax);    // 不含税总金额
                    }
                }
                break;
        }

        setOneTableRowColor("tab_data01");
        $("#TotalAmountMaterialAddedTax").text(formatMoney(_totalAmountAddedTax.toFixed(2), 1));
        $("#TotalAmountMaterialExcludedTax").text(formatMoney(_totalAmountExcludedTax.toFixed(2), 1));
        $("#TotalAmountNonMaterialAddedTax").text(formatMoney(_totalAmountNonMaterialAddedTax.toFixed(2), 1));
        $("#TotalAmountNonMaterialExcludedTax").text(formatMoney(_totalAmountNonMaterialExcludedTax.toFixed(2), 1));
        $("#TotalAmountAddedTax").text(formatMoney((_totalAmountAddedTax + _totalAmountNonMaterialAddedTax).toFixed(2), 1));
        $("#TotalAmountExcludedTax").text(formatMoney((_totalAmountExcludedTax + _totalAmountNonMaterialExcludedTax).toFixed(2), 1));

        this.AutoAdjustTableHeightForLines();   // 自适应高度设置
        $(window).resize();                     // 防止因为出现滚动条而造成画面显示问题
        $('#loading').hide();
    }
    //《按钮》搜索
    DoSearch() {
        this.FetchForPage(1);
    }
    AutoAdjustTableHeightForLines() {
        let h = $(window).height();
        let _rc = Math.floor((h - 147 - 110) / 25);
        let _dh = _rc * 25;
        let _tah = this.rows * 25;
        console.info(this.rows);
        _dh = (_tah >= _dh) ? _dh : _tah;

        $("#div_content").css({"max-height": _dh, height: _dh});
    }
    DoPrint() {
        // 得到要打印的内容
        let _print_content =
                '<table style="width:100%; margin:0 auto; text-align:left;">' +
                '    <thead>' +
                '        <tr><td colspan="10" style="font-size:16px; font-weight:bold; padding-bottom:10px; border: none;">采购合同统计报表</td></tr>' +
                '        <tr>' +
                '            <th>供应商</th>' +
                '            <th>款号</th>' +
                '            <th>材料名称</th>' +
                '            <th>材料编码</th>' +
                '            <th>材料颜色</th>' +
                '            <th>材料色号</th>' +
                '            <th>材料规格</th>' +
                '            <th>调达数量</th>' +
                '            <th>订货数量</th>' +
                '            <th>采购金额</th>' +
                '            <th>落单日期</th> ' +
                '        </tr>' +
                '    </thead>' +
                '    <tbody>' + $("#tab_data01 tbody").html() +
                '    </tbody>' +
                '</table>';
        // 生成并打印ifrme  
        let _iframe = document.createElement('IFRAME');
        _iframe.setAttribute('style', 'position:absolute; width:0px; height:0px; left:-500px; top:-500px;');
        document.body.appendChild(_iframe);
        let _doc = _iframe.contentWindow.document;
        _doc.write('<!DOCTYPE html>');
        _doc.write('<html><head><meta charset="utf-8">');
        // 引入打印的专有CSS样式
        _doc.write('<style type="text/css" media="print">' +
                '    @page {' +
                '        size: A4 landscape;' +
                //'    margin: 10mm 10mm 10mm 10mm;' +
                '        @bottom-right {font-size:9pt; content: "页 " counter(page) " / " counter(pages);}' +
                '    }' +
                '    table {border-collapse: collapse; border-spacing: 0;}' +
                '    thead {display: table-header-group;}' +
                '    th {padding: 3px 2px; font-size: 12px; font-weight: bold; text-align: center; border: 1px solid #000;}' +
                '    td {padding: 3px 2px; font-size: 12px; font-weight: normal; text-align: center; border: 1px solid #000;}' +
                '</style>');
        _doc.write('</head><body>');
        _doc.write(_print_content);
        _doc.write('</body></html>');
        _doc.close();
        _iframe.contentWindow.print();
        document.body.removeChild(_iframe);
    }
    TableToExcel(table, filename) {
        if (!table.nodeType)
            table = document.getElementById(table);
        let _content =
                '<table border="1">' +
                '    <caption>采购合同统计报表</caption>' +
                '    <thead>' +
                '        <tr>' +
                '            <td>供应商</td>' +
                '            <td>款号</td>' +
                '            <td>材料名称</td>' +
                '            <td>材料编码</td>' +
                '            <td>材料颜色</td>' +
                '            <td>材料色号</td>' +
                '            <td>材料规格</td>' +
                '            <td>调达数量</td>' +
                '            <td>订货数量</td>' +
                '            <td>采购金额</td>' +
                '            <td>落单日期</td> ' +
                '        </tr>' +
                '    </thead>' +
                '    <tbody>' + table.getElementsByTagName("tbody")[0].innerHTML +
                '    </tbody></table>';
        // 包装成一个完整的HTML文档，设置charset为urf-8以防止中文乱码
        let html = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">' +
                '<head><meta http-equiv="content-type" content="application/vnd.ms-excel; charset=utf-8" />' +
                '<style type="text/css">' +
                '    td {mso-number-format:"\@";}' +
                '    caption {' +
                '        height:30pt; font-size:14.0pt; font-weight:700; text-align:center;' +
                '    }' +
                '</style>' +
                '</head><body>' + _content + '</body></html>';

        // 实例化一个Blob对象，其构造函数的第一个参数是包含文件内容的数组，第二个参数是包含文件类型属性的对象
        let blob = new Blob([html], {type: "application/vnd.ms-excel"});
        window.URL = window.URL || window.webkitURL;
        let link = window.URL.createObjectURL(blob);
        let a = document.createElement("a");
        a.download = filename;
        a.href = link;
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
    }
}

var _Instance = null;

//-----------------------------------------------------------------------------
// 页面初始化
//-----------------------------------------------------------------------------
function onPageReady() {
    $("#search_btn").hide(); // 无需搜索，隐藏搜索按钮

    _Instance = new MyPage(0, null, null, null, null, null, null, null,
            "/financial/report/payment_for_material/fetch.html", null);

    _Instance.OnPageLoad();
}

// 画面大小或者位置移动
function onPageResize() {
    if (_Instance !== null)
        _Instance.AutoAdjustTableHeightForLines();
}
